package com.jsyl.lwbk.business.service.education.impl;

import com.jsyl.lwbk.business.dto.education.AddQuestionReporttypeDTO;
import com.jsyl.lwbk.business.mapper.education.QuestionReporttypeMapper;
import com.jsyl.lwbk.business.domain.education.QuestionReporttype;
import com.jsyl.lwbk.business.domain.education.QuestionReporttypeExample;
import com.jsyl.lwbk.business.dto.education.QuestionReporttypeListDTO;
import com.jsyl.lwbk.business.service.education.IQuestionReporttypeService;
import com.jsyl.lwbk.business.vo.education.QuestionReportTypeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author jh
 * @date 2020-11-02 16:18
 * @desc
 **/
@Service
public class QuestionReporttypeServiceImpl implements IQuestionReporttypeService {

    @Autowired
    private QuestionReporttypeMapper questionReporttypeMapper;



    @Override
    public List<QuestionReportTypeVO> findList(QuestionReporttypeListDTO questionReporttypeListDTO) {
        return questionReporttypeMapper.selectByCondition(questionReporttypeListDTO);
    }

    @Override
    public List<QuestionReporttype> findListByType(Short trainingModuleType) {
        QuestionReporttypeExample example = new QuestionReporttypeExample();
        example.createCriteria().andTrainingModuleTypeEqualTo(trainingModuleType);
        return questionReporttypeMapper.selectByExample(example);
    }

    @Override
    public QuestionReporttype findById(Long questionId) {
        QuestionReporttypeExample example = new QuestionReporttypeExample();
        example.createCriteria().andQuestionIdEqualTo(questionId);
        List<QuestionReporttype> questionReporttypes = questionReporttypeMapper.selectByExample(example);
        if(!CollectionUtils.isEmpty(questionReporttypes)){
            return questionReporttypes.get(0);
        }
        return null;
    }

    @Override
    public void deleteByQuestionId(Long questionId) {
        QuestionReporttypeExample example = new QuestionReporttypeExample();
        example.createCriteria().andQuestionIdEqualTo(questionId);
        questionReporttypeMapper.deleteByExample(example);
    }

    @Override
    @Transactional
    public void add(AddQuestionReporttypeDTO addQuestionReporttypeDTO) {
        List<Long> questionIds = addQuestionReporttypeDTO.getQuestionIds();
        for(Long questionId:questionIds) {
            QuestionReporttype questionReporttype = new QuestionReporttype();
            questionReporttype.setTrainingModuleType(addQuestionReporttypeDTO.getTrainingModuleType());
            questionReporttype.setQuestionId(questionId);
            questionReporttypeMapper.insert(questionReporttype);
        }

    }
}
